#ifndef SERVER_H_
#define SERVER_H_

#include <map>
#include <websocketpp/config/asio_no_tls.hpp>
#include <websocketpp/server.hpp>
#include "types.h"

typedef websocketpp::server<websocketpp::config::asio> WebsocketServer;
typedef WebsocketServer::message_ptr message_ptr;
using websocketpp::connection_hdl;
using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
using websocketpp::lib::bind;

struct conndata {
	std::string name;
	connection_hdl peer;
    bool myturn;
	const static int board_sz = 10;
	bool board[board_sz][board_sz];
};

class gomokusrv {
public:
	gomokusrv(int port);
    //std::string gomokusrv::bin2str_inplace(const char **bin);
	void send(connection_hdl hdl, const std::string &buf);
	void send_type(connection_hdl hdl, S2C type);
    void close(connection_hdl hdl);
	void OnOpen(connection_hdl hdl);
	void OnClose(connection_hdl hdl);
	void OnMessage(connection_hdl hdl, message_ptr msg);
	void HandleMatchReq(connection_hdl hdl, std::istringstream& in);
    void start_game(connection_hdl hdl1, connection_hdl hdl2);
	void matched(connection_hdl player, const conndata &peer);
	void HandleMyPos(connection_hdl hdl, std::istringstream& in);
private:
    WebsocketServer server;
	//TODO: use unordered_map
	std::map<connection_hdl, conndata, std::owner_less<connection_hdl> > conn_list;
	connection_hdl waiting;
};

#endif	//SERVER_H_
